﻿
using Models.Common;
using Models.Device;
using Newtonsoft.Json;

using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading;
using static Models.Common.EnumDeviceState;
using Microsoft.EntityFrameworkCore;
using GatewayServerService.Data;
using GatewayServerService.Mq;
using static Models.Common.EnumDeviceType;
using UtilsBase;
using UtilsORM.MySqlData;
using UtilsORM;
using UtilsORM.MySqlData;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using GatewayServerService.CloudCenter;

namespace GatewayServerService.CloudCenter
{
    /// <summary>
    /// 处理来自接口的请求
    /// </summary>
    public class ServiceCloodAPIControl
    {
        EFYihongIotContext efdc = new EFYihongIotContext();
        EFYihongIotContext eFIotGateway = new EFYihongIotContext();
        public void deviceControl(MQReqModel<object> httpReqModel)
        {

            ModelReceverData modelDeviceData = httpReqModel.toServerData;
            ModelRequestDeviceControl request = JsonConvert.DeserializeObject<ModelRequestDeviceControl>(modelDeviceData.content);
            if (request != null)
            {
                try
                {

                    if (request.commandName == EnumDeviceControlCommandName.deviceCommand)
                    {
                        if (string.IsNullOrEmpty(request.from))
                        {
                            request.from = "API 接口(未设置来源)";
                        }
                        new ServiceCloudDeviceCommand().sendCommandToGateWayByCommand(request.dguid, request.key, request.from, request.content);
                        //在这里增加推送到前端的websocket，提醒刷新设备开关的状态

                        ////推送到云远程
                        //new ServiceCloudRemoteManager().sendMsgDeviceStatusChange(request.dguid, request.key, request.from);

                    }
                    else if (request.commandName == EnumDeviceControlCommandName.bashCommand)
                    {
                        //系统指令
                        new ServiceCloodAPIControl_ext().sendBashCommand(request.content, request.gwcode);
                    }
                    else if (request.commandName == EnumDeviceControlCommandName.upGradeMQTTConfig)
                    {
                        //修改mqtt
                        MQTTInfo info = JsonConvert.DeserializeObject<MQTTInfo>(request.content);
                        new ServiceCloodAPIControl_ext().upGradeMQTTConfig(info, request.gwcode);
                    }
                    else if (request.commandName == EnumDeviceControlCommandName.upConfigAppSetting)
                    {
                        //修改配置
                        ModelConfigKeyValue model = JsonConvert.DeserializeObject<ModelConfigKeyValue>(request.content);
                        new ServiceCloodAPIControl_ext().upConfigAppSetting(model, request.gwcode);
                    }
                    else if (request.commandName == EnumDeviceControlCommandName.updateGradeSoftware)
                    {
                        //升级
                        new ServiceCloodAPIControl_ext().updateGradeSoftware(request.gwcode);
                    }

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
            }


        }

    }

}
